Ext.onReady(function(){
    Ext.define('json_data',{
        extend: 'Ext.data.Model',
        fields: [
             {name:'username',type:'string'},
             {name:'email',type:'string'},
             {name:'is_active',type:'boolean'},
             {name:'is_superuser',type:'boolean'},
             {name:'is_staff',type:'boolean'},
             {name:'date_joined',type:'date',format:'y年m月d日'},
             {name:'last_login',type:'date',format:'y年m月d日'}
        ]
    });
    //创建数据源
    var store = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/usercontrol/queryUserBy/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: true
    });
    //多选
    var selModel = Ext.create('Ext.selection.CheckboxModel');
    //创建Grid
    var grid = Ext.create('Ext.grid.Panel',{
        store: store,
        //height: 500,
        selModel:selModel,
        columnLines:true,
        columns: [
            {text: "账号", width: 100, dataIndex: 'username', sortable: false},
            {text: "邮箱", width: 120, dataIndex: 'email', sortable: false},
            {text: "是否激活", width: 80, dataIndex: 'is_active', sortable: false},
            {text: "超级用户", width: 80, dataIndex: 'is_superuser', sortable: false},
            {text: "是否管理", width: 80, dataIndex: 'is_staff', sortable: false},
            {text: "账号创建时间", width: 120, dataIndex: 'date_joined', sortable: false},
            {text: "上次登录时间", width: 120, dataIndex: 'last_login', sortable: false}
        ],
        title: '用户管理',
        // disableSelection: false,
        layout : 'fit',
        //frame:true,
        loadMask:true,
        region: 'north',
        renderTo: 'employee-contract-grid',
        viewConfig: {
            stripeRows: true
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            //displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[{
        		id:'searchName',
        		xtype:'textfield',
        		labelWidth: 50,
        		fieldLabel:'姓名'
        	},{
        		xtype:'button',
        		text:'查询',
        		handler:function(){
        			var params = { searchName: Ext.getCmp('username').getValue() };
        		    Ext.apply(store.proxy.extraParams, params);
        		    store.currentPage=1;
        		    store.load();
        		}
        	},{
        		xtype:'button',
        		text:'添加',
        		handler:function(){
        			form.getForm().reset();
        			win.show();
        		}
        	},{
        		xtype:'button',
        		text:'修改',
        		handler:function(){
        			form.getForm().reset();
        			var selected = grid.getSelectionModel().getSelection();
					form.loadRecord(selected[0]);
        			win.show();
        		}
        	},{
        		xtype:'button',
        		text:'删除',
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
        			var ids = [];
        			Ext.each(selected, function (item) {
        				ids.push(item.data.id);
        			});
        			alert(ids);
        		}
        	}]
        }]
    });
    

	var form = Ext.create('Ext.form.Panel', {
		width: 550,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '帐号',
				name : 'username',
				allowBlank : false,
				blankText : '请输入帐号'
			}, {
				fieldLabel : '密码',
				name : 'password',
				inputType : 'password', 
				allowBlank : false,
				blankText : '请输入密码'
			}]
		},{
			xtype:'textfield',
			fieldLabel:'邮件',
			name:'email'
		},{
			xtype:'fieldset',
			title:'账号权限',
			height: 56,
			layout:{
				align:'stretch',
				type:'hbox'
			},
			items:[{
                xtype: 'checkboxfield',
                name:'is_superuser',
                fieldLabel: '超级用户',
                anchor: '100%'
			},{
                xtype: 'checkboxfield',
                name:'is_active',
                fieldLabel: '是否激活',
                anchor: '100%'
			}]
		}]
	});

	var win = Ext.create('Ext.window.Window', {
		//title : '---',
		modal: true,
		closeAction: 'hide',
		items : [form],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (form.getForm().isValid()) {
					form.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在登录......',
						url : 'user_manage/login/',
						success : function(form, action) {
							Ext.Msg.alert('系统提示', '成功!');
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.text);
							} else {
								form.getForm().reset();
							}
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				form.getForm().reset();
				win.hide();
			}
		} ]
	});    
    
});